package action;

import info.FloorInfo;
import java.util.ArrayList;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathFactory;
import net.Connect;
import net.Process;
import org.apache.http.message.BasicNameValuePair;
import org.w3c.dom.Document;
import start.Info;

public class GetFloorInfo
{
  private static final String URL_AREA_FLOOR = Info.LoginServer + "/connect/app/exploration/floor?cyt=1";
  private static byte[] result;

  public static boolean run(FloorInfo floorInfo, boolean isClear)
    throws Exception
  {
    ArrayList al = new ArrayList();
    al.add(new BasicNameValuePair("area_id", floorInfo.id));
    try {
      result = Process.connect.connectToServer(URL_AREA_FLOOR, al);
    } catch (Exception ex) {
      throw ex;
    }
    Document doc;
    try {
      doc = Process.ParseXMLBytes(result);
    }
    catch (Exception ex)
    {
      throw ex;
    }
    try
    {
      return parse(doc, isClear);
    } catch (Exception ex) {
      throw ex;
    }
  }

  private static boolean parse(Document doc, boolean isClear) throws Exception
  {
    try {
      if (ExceptionCatch.catchException(doc)) {
        return false;
      }

      XPathFactory factory = XPathFactory.newInstance();
      XPath xpath = factory.newXPath();
      if (isClear) {
        Process.info.floorId = xpath.evaluate("//exploration_floor/floor_info_list/floor_info[progress=100]/id", doc);
        Process.info.floorCost = Integer.parseInt(xpath.evaluate("//exploration_floor/floor_info_list/floor_info[progress=100]/cost", doc));
      } else {
        Process.info.floorId = xpath.evaluate("//exploration_floor/floor_info_list/floor_info[progress!=100]/id", doc);
        Process.info.floorCost = Integer.parseInt(xpath.evaluate("//exploration_floor/floor_info_list/floor_info[progress!=100]/cost", doc));
      }
    }
    catch (Exception ex) {
      throw ex;
    }
    return true;
  }
}